﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace GUI.TimKiem
{
    public partial class frmTimKiemHangHoaTheoKho : Form
    {
        public frmTimKiemHangHoaTheoKho()
        {
            InitializeComponent();
        }
        private string hanhDong;

        public string HanhDong
        {
            get { return hanhDong; }
            set { hanhDong = value; }
        }
        private string maKiemTra;

        public string MaKiemTra
        {
            get { return maKiemTra; }
            set { maKiemTra = value; }
        }

        public frmTimKiemHangHoaTheoKho(string hanhDong, string maKiemTra)
        {
            InitializeComponent();
            this.hanhDong = hanhDong;
            this.maKiemTra = maKiemTra;
        }
        public frmTimKiemHangHoaTheoKho(string hanhDong)
        {
            InitializeComponent();
            this.hanhDong = hanhDong;
        }

        private void frmTimKiemHangHoaTheoKho_Load(object sender, EventArgs e)
        {
            try
            {
                switch (hanhDong)
                {
                    case "DanhMuc":
                        HienThiDanhMuc();
                        break;
                    case "TheoNhaCungCap":
                        this.HienThiTheoNhaCungCap();
                        break;
                    default:
                        break;
                }
            }
            catch (Exception ex)
            { string s = ex.Message; }
        }


        #region Hien Thi
        private Entities.HienThiHangHoaListview[] DanhSach;
        private Entities.HienThiHangHoaListview banghi;
        
        private void HienThiDanhMuc()
        {
            try
            {
                Entities.HienThiHangHoaListview ncc = new Entities.HienThiHangHoaListview();
                ncc.HanhDong = "2";
                ncc.MaKho = "";
                ncc.MaNhaCungCap = "";
                ncc.MaHoaDon = "";
                this.DanhSach = Common.StaticVariable.InterfaceObject.SelectChiTietHangHoaCacBang(ncc);
                dgvHienThi.DataSource = DanhSach;
                dgvHienThi.Rows[0].Selected = true;
                Gridview();
            }
            catch (Exception ex)
            {
                DanhSach = null;
                banghi = null;
                Gridview();
                string s = ex.Message;
            }
        }

        private void HienThiTheoNhaCungCap()
        {
            try
            {
                Entities.HienThiHangHoaListview ncc = new Entities.HienThiHangHoaListview();
                ncc.HanhDong = "3";
                ncc.MaKho = "";
                ncc.MaNhaCungCap = this.maKiemTra;
                ncc.MaHoaDon = "";
                this.DanhSach = Common.StaticVariable.InterfaceObject.SelectChiTietHangHoaCacBang(ncc);
                dgvHienThi.DataSource = DanhSach;
                dgvHienThi.Rows[0].Selected = true;
                Gridview();
            }
            catch (Exception ex)
            {
                DanhSach = null;
                banghi = null;
                Gridview();
                string s = ex.Message;
            }
        }


        private void Gridview()
        {
            try
            {
                if (DanhSach == null)
                {
                    Entities.HienThiHangHoaListview[] nv = new Entities.HienThiHangHoaListview[0];
                    dgvHienThi.DataSource = nv;
                }
                for (int i = 0; i < dgvHienThi.ColumnCount; i++)
                {
                    dgvHienThi.Columns[i].Visible = false;
                }
                dgvHienThi.Columns["MaHangHoa"].Visible = true;
                dgvHienThi.Columns["MaHangHoa"].HeaderText = "Mã hàng hóa";
                dgvHienThi.Columns["TenHangHoa"].Visible = true;
                dgvHienThi.Columns["TenHangHoa"].HeaderText = "Tên hàng hóa";
                dgvHienThi.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
                dgvHienThi.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
            }
            catch (Exception ex)
            { string s = ex.Message; }
        }
        #endregion

        private string MaHangHoa= string.Empty;
        private void GetRow()
        {
            try
            {
                if (DanhSach == null) { return; }
                for (int i = 0; i < this.DanhSach.Length; i++)
                {
                    if (DanhSach[i].MaHangHoa == this.MaHangHoa)
                    {
                        banghi = DanhSach[i];
                        break;
                    }
                    else
                    { continue; }
                }
            }
            catch (Exception ex)
            {
                string s = ex.Message;
            }
        }

        #region Thoát
        private void Thoat()
        {
            try
            {
                System.Windows.Forms.DialogResult ketqua = System.Windows.Forms.MessageBox.Show("Bạn có chắc chắn muốn thoát không ?", "Thông báo", System.Windows.Forms.MessageBoxButtons.YesNo);
                {
                    if (ketqua == System.Windows.Forms.DialogResult.Yes)
                    {
                        Common.StaticVariable.chitietkhohang = null;
                        this.Close();
                    }
                }
            }
            catch (Exception)
            { }
        }
        #endregion

        private void Chon()
        {
            try
            {
                if (this.banghi == null)
                {
                    MessageBox.Show("Bạn phải chọn hàng hóa.", "Thông báo");
                    return;
                }
                Common.StaticVariable.ChiTietHangHoaCacBang = banghi;
                this.Close();
            }
            catch (Exception ex)
            { string s = ex.Message; }
        }

        private void btnThoat_Click(object sender, EventArgs e)
        {
            this.Thoat();
        }

        private void btnChon_Click(object sender, EventArgs e)
        {
            this.Chon();
        }

        private void dgvHienThi_DoubleClick(object sender, EventArgs e)
        {
            this.Chon();
        }

        private void dgvHienThi_SelectionChanged(object sender, EventArgs e)
        {
            if (dgvHienThi.Rows.Count <= 0)
            { return; }
            MaHangHoa = dgvHienThi.CurrentRow.Cells["MaHangHoa"].Value.ToString();
            this.GetRow();
        }
    }
}
